/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lintulaskenta.utils;


import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;

public class Selection implements SelectionInterface {

	public static class SelectionOptionImple implements _SelectionOption {

		private final String	value;
		private final String	description;

		public SelectionOptionImple(String value, String description) {
			this.value = value.toUpperCase();
			this.description = description;
		}

		@Override
		public String getValue() {
			return value;
		}

		@Override
		public String getText() {
			return description;
		}

	}

	private final String									name;
	private final LinkedHashMap<String, _SelectionOption>	options;

	public Selection(String name) {
		this.name = name.toLowerCase();
		options = new LinkedHashMap<String, _SelectionOption>();
	}

	public Selection(Selection selection) {
		this(selection.getName());
		for (_SelectionOption o : selection.getOptions()) {
			this.addOption(o);
		}
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Collection<_SelectionOption> getOptions() {
		return options.values();
	}

	public void addOption(_SelectionOption o) {
		options.put(o.getValue(), o);
	}

	@Override
	public String get(String optionValue) throws IllegalArgumentException {
		if (optionValue.equals("")) return "";
		if (!containsOption(optionValue)) throw new IllegalArgumentException("Selection " + name + " does not define value '" + optionValue + "'.");
		return options.get(optionValue).getText();
	}

	@Override
	public boolean containsOption(String optionValue) {
		return options.containsKey(optionValue);
	}

	@Override
	public Iterator<_SelectionOption> iterator() {
		return options.values().iterator();
	}

	public void removeOption(String optionValue) {
		if (!this.containsOption(optionValue)) {
			throw new IllegalArgumentException("No option with value " + optionValue +" defined");
		}
		options.remove(optionValue);
	}

}
